# encoding: utf-8


import time, os
from .db import Sqlite3cls

from apps.run_cmd import RunCmd

from multiprocessing import Process, current_process, Pool
from apps.log import LOGGER


# 中间代理调用处理类
def runcmd(task, running_work):
    rcmd = RunCmd(task, running_work)
    LOGGER.info(rcmd.pre_command())
    LOGGER.info(rcmd.post_command())
    rcmd.finish()


def worker(running_work):
    LOGGER.info('启动消费者')
    while True:
        time.sleep(2)
        with Sqlite3cls() as sqlite3:
            taskdict = sqlite3.task_getone()
        if taskdict:
            LOGGER.info(f'取出任务:{taskdict}')
            p = Process(target=runcmd, args=(taskdict, running_work), name=taskdict.get("task_name"))
            p.start()
            running_work[taskdict.get("task_name")] = taskdict
        else:
            LOGGER.debug("没有任务")
